//MINI UI解析
mini.parse();

//模板处理器类
function WebTplHandler() {
	this.tplTree = mini.get("tplTree");
	this.tplGrid = mini.get("tplGrid");
	this.selectedValue = null;
	this.firstLoad = true;
	this.wmCfg = new WebMgrConfig();
	
	this.tplGrid.url = __root + "/getTplFiles.do";
	
	this.loadTplTree = function() {
		this.tplTree.load(__root + "/getTplDirectorys.do");
	};
	
	this.expandParentNode = function(node) {
		var parent = this.tplTree.getParentNode(node);
		if (parent && !parent.expanded) {
			this.expandParentNode(parent);
			this.tplTree.expandNode(parent);
		}
	};
	
	this.loadTplGrid = function() {
		this.tplGrid.load({ parentPath : this.selectedValue });
	};
	
	this.refreshTplGrid = function() {
		instance.tplGrid.load({ parentPath : instance.selectedValue });
	};
	
	this.rename = function(name) {
		miniWin.afreshNewWin();
		miniWin.setParams(360, 160, "重命名[<font color='blue'>" + name + "</font>]");
		miniWin.setOption("bodyStyle","padding:0px;");
		
		miniWin.load("editName.jsp", function() {
			var iframe = miniWin.getIFrameEl();
	        var data = { forres : false, iscreate : false, name : name, parent : instance.selectedValue };
	        iframe.contentWindow.instance.load(data);
		}, function(action) {
			if (action == "confirm") {
				instance.loadTplGrid();
			}
		});
		
		miniWin.show();
	};
	
	this.edit = function(name) {
		miniWin.afreshNewWin();
		miniWin.setParams(540, 440, "编辑文件[<font color='blue'>" + name + "</font>]");
		miniWin.setOption("bodyStyle","padding:0px;");
		miniWin.setOnButtonClick();
		
		miniWin.load("editTemplate.jsp", function() {
			var iframe = miniWin.getIFrameEl();
	        var data = { iscreate : false, name : name, parent : instance.selectedValue };
	        iframe.contentWindow.instance.load(data, instance.refreshTplGrid);
	        miniWin.setOnClose(iframe.contentWindow.instance.onclose);
		}, function(action) {
			if (action == "save") {
				instance.loadTplGrid();
			}
		});
		
		miniWin.show();
	};
	
	this.remove = function(path) {
		mini.confirm("确定要删除选定的模板？", "系统提示", function(action) {
			if (action == "ok") {
				var paths = [{ path : path }];
				instance.removes(paths);
			}
		});
	};
	
	this.removes = function(paths) {
		var json = mini.encode(paths);
		$.ajax({
			url : __root + "/delTplFiles.do", 
			type : "POST", 
			data: {
	        	data: json,
	        	lslUser: gbOperateUser,
	    		lslAddress: gbOperateIp
	        },
			dataType : "json", 
			success : function(result) {
				if (result.issuccess) {
					instance.loadTplGrid();
				} else {
					mini.alert(result.message, "系统提示");
				}
			}, 
			error : function (request) {
				mini.alert(request.responseText, "系统提示");
			}
		});
	};
};

var instance = new WebTplHandler();		//创建模板处理器全局实例
instance.loadTplTree();					//加载模板树

//定义模板树加载完成事件处理程序
function onTplTreeLoaded(e) {
	var selectedNode = null;
	if (instance.selectedValue) {
		selectedNode = this.getNode(instance.selectedValue);
	} else {
		selectedNode = this.getRootNode().children[0];
		instance.selectedValue = selectedNode.path;
	}
	
	instance.expandParentNode(selectedNode);
	this.selectNode(selectedNode);
	
	if (instance.firstLoad) {
		instance.loadTplGrid();
	}
}

//定义模板树节点点击事件处理程序
function onTplTreeNodeClick(e) {
	instance.selectedValue = e.node.path;
	instance.loadTplGrid();
}

//定义模板树刷新菜单事件处理程序
function onTplTreeReloadClick(e) {
	instance.firstLoad = false;
	instance.loadTplTree();
}

//定义模板表格单元格绘制事件处理程序
function onTplGridDrawCell(e) {
	var html = "";
	
	switch (e.column.field) {
		case "icon":
		{
			var type = e.record.type;
			
			if (!instance.wmCfg.tplIcon[type]) {
				type = instance.wmCfg.tplIcon.unknown;
			}
			
			html += "<span><div class='icon-" + type + "'></div></span>";
			break;
		}
		case "last":
			html += e.record.last.formatDate();
			break;
		case "operator":
		{
			var path = e.record.path;
			var name = e.record.name;
			var type = e.record.type;
			
			html += "<span><a style='cursor: pointer;' onclick='instance.rename(\"" + name + "\");'>重命名</a></span>";
			html += "<span>&nbsp;|&nbsp;</span>";
			if (type == instance.wmCfg.tplIcon.html) {
				html += "<span><a style='cursor: pointer;' onclick='instance.edit(\"" + name + "\");'>编辑</a></span>";
			} else {
				html += "<span><a style='cursor: default; color: gray;'>编辑</a></span>";
			}
			html += "<span>&nbsp;|&nbsp;</span>";
			html += "<span><a style='cursor: pointer;' onclick='instance.remove(\"" + path + "\");'>删除</a></span>";
			break;
		}
	}
	
	if (html) e.cellHtml = html;
}

//定义模板表格新建目录点击事件处理程序
function onTplGridFolderClick(e) {
	miniWin.afreshNewWin();
	miniWin.setParams(343, 154, "新建目录");
	miniWin.setOption("bodyStyle","padding:0px;");
	
	miniWin.load("editName.jsp", function() {
		var iframe = miniWin.getIFrameEl();
        var data = { forres : false, iscreate : true, name : "", parent : instance.selectedValue };
        iframe.contentWindow.instance.load(data);
	}, function(action) {
		if (action == "confirm") {
			instance.loadTplGrid();
		}
	});
	
	miniWin.show();
}

//定义模板表格新建文件点击事件处理程序
function onTplGridFileClick(e) {
	miniWin.afreshNewWin();
	miniWin.setParams(540, 465, "新建文件");
	miniWin.setOption("bodyStyle","padding:0px;");
	miniWin.setOnButtonClick();
	
	miniWin.load("editTemplate.jsp", function() {
		var iframe = miniWin.getIFrameEl();
        var data = { iscreate : true, name : "", parent : instance.selectedValue };
        iframe.contentWindow.instance.load(data, instance.refreshTplGrid);
        miniWin.setOnClose(iframe.contentWindow.instance.onclose);
	}, function(action) {
		if (action == "save") {
			instance.loadTplGrid();
		}
	});
	
	miniWin.show();
}

//刷新数据表格点击事件处理程序
function afresh(){
	instance.loadTplGrid();
}

//定义模板表格上传文件点击事件处理程序
function onTplGridUploadClick(e) {
	miniWin.afreshNewWin();
	miniWin.setParams(356, 154, "上传文件");
	miniWin.setOption("bodyStyle","padding:0px;");
	
	miniWin.load("uploadFile.jsp", function() {
		var iframe = miniWin.getIFrameEl();
        var data = {
        	forres : false, 
        	parent : instance.selectedValue, 
        	type : instance.wmCfg.tplType, 
        	size : instance.wmCfg.tplSize
        };
        iframe.contentWindow.instance.load(data);
	}, function(action) {
		if (action == "upload") {
			instance.loadTplGrid();
		}
	});
	
	miniWin.show();
}

//定义模板表格删除点击事件处理程序
function onTplGridRemoveClick(e) {
	var rows = instance.tplGrid.getSelecteds();
	if (rows.length > 0) {
		mini.confirm("确定要删除选定的模板？", "系统提示", function(action) {
			if (action == "ok") {
				var paths = [];
				for (var i = 0; i < rows.length; i+=1) {
					paths[i] = { path : rows[i].path};
				}
				
				instance.removes(paths);
			}
		});
	} else {
		mini.alert("请选择要删除的模板！", "系统提示");
	}
}
